package org.example;
import java.util.Scanner;

public class Practice_2 {

    public static void main(String[] args) {
        Node[] nodes = create();

        Node root1 = nodes[0];
        Node root2 = nodes[1];

        Node root3 = sum(root1,root2);

        output(root1);
        output(root2);
        output(root3);
    }


    static class Node{
        Node next;
        int coefficient; //系数
        int index; //指数

        public Node(int coefficient, int index) {
            this.coefficient = coefficient;
            this.index = index;
        }

        public Node(Node next, int coefficient, int index) {
            this.next = next;
            this.coefficient = coefficient;
            this.index = index;
        }

        public Node() {

        }
    }

    public static Node[] create(){
        Node[] nodes = new Node[2];

        Node root1 = new Node();
        Node root2 = new Node();

        Scanner scanner = new Scanner(System.in);
        Node p = root1;
        while (scanner.hasNext()){
            String data = scanner.next();
            String[] values = data.split(",");
            if (values[0].equals("-100")){
                break;
            }
            Node node = new Node(Integer.parseInt(values[0]),
                    Integer.parseInt(values[1]));
            p.next = node;
            p = p.next;
        }
        nodes[0] = root1;

        p = root2;
        while (scanner.hasNext()){
            String data = scanner.next();
            String[] values = data.split(",");
            if (values[0].equals("-100")){
                break;
            }
            Node node = new Node(Integer.parseInt(values[0]),
                    Integer.parseInt(values[1]));
            p.next = node;
            p = p.next;
        }
        nodes[1] = root2;

        return nodes;
    }

    public static Node sum(Node add1, Node add2) {
        Node root = new Node();     //新链表
        Node p = root;              //新链表的指针

        Node p1 = add1.next,p2 = add2.next;

        int coefficient,index;

        while (p1 != null || p2 != null){
            if (p1 == null){
                index = p2.index;
                coefficient = p2.coefficient;
                p2 = p2.next;
            }else if (p2 == null || p1.index < p2.index){
                index = p1.index;
                coefficient = p1.coefficient;
                p1 = p1.next;
            }else if(p1.index > p2.index){
                index = p2.index;
                coefficient = p2.coefficient;
                p2 = p2.next;
            }else {
                index = p1.index;
                coefficient = p1.coefficient + p2.coefficient;

                p1 = p1.next;
                p2 = p2.next;
            }
            if (coefficient != 0){
                Node node = new Node(coefficient,index);
                p.next = node;
                p = p.next;
            }
        }

        return root;
    }

    public static void output(Node root){
        if(root.next == null){
            System.out.println(0);
            return;
        }

        Node p = root.next;
        while (p != null){
            String data = p.coefficient+","+p.index+" ";
            System.out.print(data);
            p = p.next;
        }

        System.out.println();
    }


}
